# -*- coding:utf-8 -*-
"""
Created on 2011-2-7

@author: summit
"""
import wx
import struct 

def getOverlay(width, height, color, alpha):
    """
    Method: getOverlay(width, height, color, alpha)
    Create an overlay of given color with given alpha
    """
    width = abs(width)
    height = abs(height)
    print "Generating overlay",width,height,color
    size = width * height * 3
    formatString = "%ds" % size
    red, green, blue = color
    structStr = chr(red) + chr(green) + chr(blue)
    structStr = (width * height) * structStr
    structString = struct.pack(formatString, structStr)
    img = wx.EmptyImage(width, height)
    img.SetData(structString)
    size = width * height
    structStr = chr(alpha)
    formatString = "%ds" % size
    structStr = size * structStr
    structString = struct.pack(formatString, structStr)
    img.SetAlphaData(structString)
    return img
    
def getOverlayBorders(width, height, color, alpha, lineWidth = 1):
    """
    Create borders for an overlay that are only very little transparent
    """
    width = abs(width)
    height = abs(height)
    size = width * height * 3
    formatString = "%ds" % size    
    red, green, blue = color
    structStr = chr(red) + chr(green) + chr(blue)
    structStr = (width * height) * structStr
    structString = struct.pack(formatString, structStr)
    img = wx.EmptyImage(width, height)
    img.SetData(structString)
    size = width * height
    structStr = chr(0)
    formatString = "%ds" % size    
    structStr = size * structStr
    structString = struct.pack(formatString, structStr)
    structString = chr(alpha) * (2 * width) + structString[2 * width: ]
    lengthOfStructStr = len(structString)
    structString = structString[: lengthOfStructStr  - (2 * width)] + chr(alpha) * 2 * width
    twochar = chr(alpha) + chr(alpha)
    for i in range(0, width * height, width):
        if i:
            structString = structString[: i - 2] + 2 * twochar + structString[i + 2: ]
        else:
            structString = structString[: i] + twochar + structString[i + 2: ]
    img.SetAlphaData(structString)
    return img

if __name__ == "__main__":
    import vtk
    from vtk.util.misc import vtkGetDataRoot
    app = wx.PySimpleApp()
    VTK_DATA_ROOT = vtkGetDataRoot()
    
    reader = vtk.vtkBMPReader()
    reader.SetFileName('../data/twocells.bmp')
    reader.Update()
    
    borders = getOverlayBorders(200, 150, (0, 0, 255), 80)
   # borders = borders.ConvertToBitmap()
    borders.SaveFile("C://borders.png", wx.BITMAP_TYPE_PNG )
    
    overlay = getOverlay(200, 150, (0, 0, 255), 80)
   # overlay = overlay.ConvertToBitmap()
    overlay.SaveFile("C://overlay.png", wx.BITMAP_TYPE_PNG )
    